import Vue from 'vue'
import VueRouter from 'vue-router'
import { gettoken } from '@/utils/storage.js'//to

Vue.use(VueRouter)

const routes = [

  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue')
  },
  {
    path: '/',
    name: 'layout',
    redirect: '/dashboard',
    component: () => import('../views/layout/index.vue'),
    children: [
      {
        //二级数据看板页面
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('../views/dashBoard/index.vue')
      },
      {
        //二级文章管理页
        path: '/article',
        name: 'article',
        component: () => import('../views/article/index.vue')
      },

    ]
  },
]



const router = new VueRouter({
  routes
})

let whilelist = ['/login']
router.beforeEach((to, from, next) => {
  let token = gettoken()
  if (token) {
    next()
  } else {
    if (whilelist.includes(to.path)) {
      next()
    } else {
      next('/login')
    }

  }
})
//保存原型对象的Push
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push (location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)
// }

export default router
